import base64
import json
import logging
import os
import shutil
import tempfile
import threading
import traceback
from xml.etree import ElementTree as ET
import zipfile

from functools import wraps
from contextlib import closing
from decorator import decorator

import psycopg2

import odoo
from odoo import SUPERUSER_ID
from odoo.exceptions import AccessDenied
import odoo.release
import odoo.sql_db
import odoo.tools
from odoo.sql_db import db_connect
from odoo.release import version_info
from odoo.tools import pycompat

_logger = logging.getLogger(__name__)

class DatabaseExists(Warning):
    pass


def check_db_management_enabled(method):
    def if_db_mgt_enabled(method, self, *args, **kwargs):
        pass
    return decorator(if_db_mgt_enabled, method)

def check_super(passwd):
    pass

def _initialize_db(id, db_name, demo, lang, user_password, login='admin', country_code=None, phone=None):
    pass

def _create_empty_database(name):
    pass

@check_db_management_enabled
def exp_create_database(db_name, demo, lang, user_password='admin', login='admin', country_code=None, phone=None):
    pass

@check_db_management_enabled
def exp_duplicate_database(db_original_name, db_name):
    pass

def _drop_conn(cr, db_name):
    pass

@check_db_management_enabled
def exp_drop(db_name):
    pass

@check_db_management_enabled
def exp_dump(db_name, format):
    pass

@check_db_management_enabled
def dump_db_manifest(cr):
    pass

@check_db_management_enabled
def dump_db(db_name, stream, backup_format='zip'):
    pass

@check_db_management_enabled
def exp_restore(db_name, data, copy=False):
    pass

@check_db_management_enabled
def restore_db(db, dump_file, copy=False):
    pass

@check_db_management_enabled
def exp_rename(old_name, new_name):
    pass

@check_db_management_enabled
def exp_change_admin_password(new_password):
    pass

@check_db_management_enabled
def exp_migrate_databases(databases):
    pass

@odoo.tools.mute_logger('odoo.sql_db')
def exp_db_exist(db_name):
    pass

def list_dbs(force=False):
    if not odoo.tools.config['list_db'] and not force:
        pass

    if not odoo.tools.config['dbfilter'] and odoo.tools.config['db_name']:
        pass

    chosen_template = odoo.tools.config['db_template']
    templates_list = tuple(set(['postgres', chosen_template]))
    db = odoo.sql_db.db_connect('postgres')
    with closing(db.cursor()) as cr:
        try:
            cr.execute("select datname from pg_database where datdba=(select usesysid from pg_user where usename=current_user) and not datistemplate and datallowconn and datname not in %s order by datname",(templates_list,))
            res = [odoo.tools.ustr(name) for (name,) in cr.fetchall()]
        except Exception:
            _logger.exception('Listing databases failed:')
            res = []
    return res

def list_db_incompatible(databases):
    pass

def exp_list(document=False):
    pass

def exp_list_lang():
    pass

def exp_list_countries():
    list_countries = []
    root = ET.parse(os.path.join(odoo.tools.config['root_path'], 'addons/base/data/res_country_data.xml')).getroot()
    for country in root.find('data').findall('record[@model="res.country"]'):
        name = country.find('field[@name="name"]').text
        code = country.find('field[@name="code"]').text
        list_countries.append([code, name])
    return sorted(list_countries, key=lambda c: c[1])


def exp_server_version():
    pass

def dispatch(method, params):
    pass
